package DAOs;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import classes.Download;



public class DAODownload extends DAODefault{


public void createDownload(Download download){
		
		String sql = "INSERT INTO download"+
					  "(idAparelhos, idApp ) "
				+"VALUES(?)";
		
		try {
			stmt = conn.prepareStatement(sql);
			
			stmt.setInt(1, download.getIdAparelho());
			stmt.setInt(2, download.getIdApp());
			
			stmt.execute();
			
			stmt.close();
			
		} catch (SQLException e) {
			
			throw new RuntimeException(e);
		}
	}
	
	public ArrayList<Download> readDownload(){
		ArrayList<Download> download = new ArrayList<>();
		String sql = "SELECT * FROM Download";
		
		try {
			stmt = conn.prepareStatement(sql);
			ResultSet rs = stmt.executeQuery();
			while(rs.next()){
				Download download2 = new Download();
				download2.setIdDownload((rs.getInt("idDownload")));
				download2.setIdAparelho((rs.getInt("idAparelho")));
				download2.setIdApp((rs.getInt("idApp")));
			}
			return download;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
		
	}

	public ArrayList<Download> searchDownload(String dado){
		
		try{
			ArrayList<Download> download = new ArrayList<>();

		
		String sql = "SELECT * FROM Download WHERE idDownload = ?";
		
		
			stmt = conn.prepareStatement(sql);
			

	
			stmt.setString(1, dado);
			
		
			
			ResultSet rs = stmt.executeQuery();
			
			while(rs.next()){
				
				Download download2 = new Download();
				
				download2.setIdDownload((rs.getInt("idDownload")));
				download2.setIdApp((rs.getInt("idApp")));
				download2.setIdAparelho((rs.getInt("idAparelho")));
				
				download.add(download2);
			}
			
			return download;
		}
		catch (SQLException e){
			throw new RuntimeException(e);
		}
	}

	public void deleteDownload(Download download){
		try{
			String sql = "DELETE FROM download WHERE idDownload=?";
			
			stmt = conn.prepareStatement(sql);
			stmt.setInt(1, download.getIdDownload());
			stmt.execute();
			stmt.close();
		}
		catch (SQLException e){
			throw new RuntimeException(e);
		}
	}

	public void updateDownload(Download download){
		
		String sql = "UPDATE download SET " + 
				"nome = ? "+
				"where idDownload = ?"; 

					  
		
		try {
			stmt = conn.prepareStatement(sql);
			
			stmt.setInt(1, download.getIdAparelho());
			stmt.setInt(2, download.getIdApp());
			stmt.setInt(5, download.getIdDownload());
			
			stmt.execute();

			stmt.close();

			
			
		} catch (SQLException e){
			throw new RuntimeException(e);

		}
		
	}
	
}
